<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-11-21 14:32:14
 * @Description: 奖项组的 model
 */
class ActivityAwardGroup extends Eloquent
{
    protected $table = "activity_award_group";


    public static $status = array(
        "1"=>"启用",
        "0"=>"停用",
    );
    public function created_byname()
    {
        return $this->hasOne('MktUser',"id","created_by");
    }
    public function award(){
        return $this->hasMany('ActivityAward',"award_group_id","id");
    }
    /**
     * @Author:      kc
     * @DateTime:    2016-11-22 14:00:29
     * @Description: 保存 奖项组和奖项
     */
    public static function PrizeCreate($prize,$user_id,&$response = array()){
        DB::beginTransaction();
        $group_id = ActivityAwardGroup::insertGetId([
                'name' => $prize['name'],
                'status' => $prize['status'],
                'created_at' => date("Y-m-d H:i:s",time()),
                'updated_at' => date("Y-m-d H:i:s",time()),
                'created_by' => $user_id,
            ]);
        if($group_id){
            $data = json_decode($prize['prize'],true);
            if(!empty($data)){
                foreach($data as $v){
                    if(!empty($v)){
                            $name_unique = ActivityAward::where('name',$v['name'])
                                ->where('award_group_id',$group_id)
                                ->first();
                            if(count($name_unique) > 0){
                                DB::rollBack();
                                $response = Lang::get("mkt_error.mkt_award_exit_error_610001");
                                return null;
                            }
                            try{
                                $activity_award = new ActivityAward();
                                $activity_award->award_group_id = $group_id;
                                $activity_award->name = $v['name'];
                                $activity_award->max_number = $v['max_number'];
                                $activity_award->probability = bcdiv($v['probability'],100,3);
                                $activity_award->prize_name = $v['prize_name'];
                                $activity_award->prize_type = 0;
                                $activity_award->prize_id = $v['prize_id'];
                                $activity_award->created_by = $user_id;
                                $activity_award->save();
                            }catch (Exception $e){
                                DB::rollBack();
                                $response = Lang::get("mkt_error.mkt_award_save_error_610002");
                                return null;
                            }
                        }
                    }
                }
           $response['result']['id'] = $group_id;
            DB::commit();
        }else{
            $response = Lang::get("mkt_error.mkt_award_save_error_610002");
            return null;
        }
    }

}